layui.use(['form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;

    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/tDispatch/detail?id=" + Feng.getUrlParam("id"));
    var result = ajax.start();
	if(result.data.loadingType == "自装"){
		$("#loadingNumbers").val("")
		$("#loadingNumberdiv").hide()
	}
    form.val('tDispatchForm', result.data);
	$("#loadingNumbers").val(result.data.loadingNumber)
    //执行一个laydate实例
    laydate.render({
      elem: '#startDate' //指定元素
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tDispatch/editItem", function (data) {
    		if (data.success) {
				parent.submit = 1
            	Feng.success("提交成功!"+data.data);
		    	var Index = parent.layer.getFrameIndex(window.name);
		        parent.layer.close(Index);
			} else {
                Feng.error("提交失败！"+data.message);
			}
        }, function (data) {
            Feng.error("更新失败！" + data.responseJSON.message)
        });
		data.field.loadingNumbers = data.field.loadingNumbers.replace('，',',')
		data.field.loadingNumbers = data.field.loadingNumbers.replace('.',',')
		data.field.loadingNumbers = data.field.loadingNumbers.replace('。',',')
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    
    form.on('select(loadingType)',function(){
		var loadingType = $("#loadingType").val();
		if(loadingType == "仓库"){
			$("#loadingNumberdiv").show()
		} 
		if(loadingType == "自装"){
			$("#loadingNumbers").val("")
			$("#loadingNumberdiv").hide()
		} 
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});